//
//  YLZYHErrorView.m
//  YiKaTongAPP
//
//  Created by dev on 2017/10/17.
//  Copyright © 2017年 YLZ-MAC. All rights reserved.
//

#import "YLZYHErrorView.h"

@implementation YLZYHErrorView


-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self createView];
        [self addConstraint];
    }
    return self;
}

-(void)initData
{
    
}

-(void)showView
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
}

-(void)showViewInSuperview:(UIView *)superview
{
    [superview addSubview:self];
    self.frame = superview.bounds;
}


-(void)dismissView
{
    [self removeFromSuperview];
}


-(void)createView
{
    self.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.errorImageView];
    
    [self addSubview:self.errorLabel];
    
    [self addSubview:self.reloadBtn];
}


-(void)addConstraint
{
    [self.errorImageView setFrame:CGRectMake(0, 0, VERTICAL_SCREEN_WIDTH, 172 * WIDTHRATE)];
    
    [self.errorLabel setFrame:CGRectMake(0, self.errorImageView.frame.origin.y+self.errorImageView.frame.size.height+10, VERTICAL_SCREEN_WIDTH, 27)];
    
    [self.reloadBtn setFrame:CGRectMake((VERTICAL_SCREEN_WIDTH - 116)/2.f, self.errorLabel.frame.origin.y+self.errorLabel.frame.size.height+24, 116, 40)];
}

#pragma mark lazy loading

-(UIImageView *)errorImageView
{
    if (!_errorImageView) {
        _errorImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_info"]];
        
    }
    return _errorImageView;
}

-(UILabel *)errorLabel
{
    if (!_errorLabel) {
        _errorLabel = [[UILabel alloc]init];
        _errorLabel.textAlignment = NSTextAlignmentCenter;
        _errorLabel.font = [UIFont systemFontOfSize:15];
        _errorLabel.textColor = COLOR(180, 180, 180, 1);
        _errorLabel.text = @"网络异常";
    }
    return _errorLabel;
}

-(UIButton *)reloadBtn
{
    if (!_reloadBtn) {
        _reloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_reloadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_reloadBtn setTitle:@"重新加载" forState:UIControlStateNormal];
        _reloadBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_reloadBtn setBackgroundColor:ColorFromHex(0x5087ff)];
        [_reloadBtn.layer setCornerRadius:20];
        [_reloadBtn addTarget:self action:@selector(reloadAction) forControlEvents:UIControlEventTouchUpInside];
        
        _reloadBtn.hidden = YES;
    }
    return _reloadBtn;
}

-(void)reloadAction
{
    if (self.reloadBlock) {
        self.reloadBlock();
    }
}

-(void)setReloadBlock:(YLZYHNetReloadBlock)reloadBlock
{
    if (reloadBlock) {
        _reloadBlock = reloadBlock;
        self.reloadBtn.hidden = NO;
    }
}


-(void)setButtonTitle:(NSString *)btnTil withBlock:(YLZYHNetReloadBlock)reloadBlock
{
    self.reloadBtn.titleLabel.text = btnTil;
    [self.reloadBtn setTitle:btnTil forState:UIControlStateNormal];
    [self setReloadBlock:reloadBlock];
}

#pragma mark lazy loading

-(UIView *)mainView
{
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
        _mainView.backgroundColor = [UIColor clearColor];
    }
    return _mainView;
}

-(UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor clearColor];
        _bgView.userInteractionEnabled = YES;
        
        [YHGestureHelper addTapGesture:_bgView number:1 Target:self Sel:@selector(dismissView)];
    }
    return _bgView;
}

@end
